<template>
  <a-modal
    title="新增房产楼层表"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
         <a-form-item>
          <a-input  v-show="false" v-decorator="['id']" />
        </a-form-item>
        <a-form-item>
          <a-input  v-show="false" v-decorator="['estateid']" />
        </a-form-item>
        <a-form-item
          label="所屬房產"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-input placeholder="" v-decorator="['realName']" :disabled="true"/>
        </a-form-item>
        <a-form-item
          label="樓層號"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
         <a-select style="width: 100%" placeholder="請選擇樓層號" v-decorator="['code', {rules: [{ required: true, message: '請選擇樓層號' }]}]">
            <a-select-option v-for="(item,index) in floorListData" :key="index" :disabled="item.disabled" :value="item.code">{{ item.code }}樓</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { busRealEstateFloorAdd, busRealEstateFloorList } from '@/api/modular/main/busrealestatefloor/busRealEstateFloorManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        floorListData: [],
        estateid: ''
      }
    },
    methods: {
      // 初始化方法
      add (id, name) {
        this.estateid = id
        this.visible = true
        this.getDataList()
        setTimeout(() => {
          this.form.setFieldsValue({
            id: '',
            estateid: id,
            realName: name
        })
        }, 100)
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            busRealEstateFloorAdd(values).then((res) => {
              if (res.code === 200) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('新增失败')// + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.estateid = ''
        this.visible = false
      },
      getDataList() {
        busRealEstateFloorList(this.estateid).then((res) => {
          if (res.code === 200) {
            this.floorListData = res.data
          } else {
            this.$message.error(res.msg)
          }
        })
      }
    }
  }
</script>
